package cn.edu.sgu.www.mhxysy.redis.repository.impl;

import cn.edu.sgu.www.mhxysy.consts.RedisKeyPrefixes;
import cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuMapper;
import cn.edu.sgu.www.mhxysy.redis.RedisUtils;
import cn.edu.sgu.www.mhxysy.redis.repository.RedisRepository;
import cn.edu.sgu.www.mhxysy.util.TimerUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Component
public class ChongwuRepository implements RedisRepository {

    private final RedisUtils redisUtils;
    private final ChongwuMapper chongwuMapper;

    public ChongwuRepository(RedisUtils redisUtils, ChongwuMapper chongwuMapper) {
        this.redisUtils = redisUtils;
        this.chongwuMapper = chongwuMapper;
    }

    @Override
    public void put(String id, Object value) {
        String key = RedisKeyPrefixes.PREFIX_CHONGWU + id;

        if (value == null) {
            redisUtils.set(key, null);
        } else {
            redisUtils.set(key, JSON.toJSONString(value));
        }

        redisUtils.expire(key, 7, TimeUnit.DAYS);
    }

    @Override
    public Chongwu selectById(String id) {
        String key = RedisKeyPrefixes.PREFIX_CHONGWU + id;

        if (redisUtils.hasKey(key)) {
            String value = redisUtils.get(key);

            log.debug("命中缓存{}", key);

            if (value != null) {
                return JSON.parseObject(value, Chongwu.class);
            }

            return null;
        }

        Chongwu chongwu = chongwuMapper.selectById(id);

        put(id, chongwu);

        return chongwu;
    }

    @Override
    public void putList(Object value) {
        String key = RedisKeyPrefixes.PREFIX_CHONGWUS;

        redisUtils.set(key, JSON.toJSONString(value), 7, TimeUnit.DAYS);
    }

    @Override
    public List<Chongwu> selectList() {
        String key = RedisKeyPrefixes.PREFIX_CHONGWUS;

        if (redisUtils.hasKey(key)) {
            String value = redisUtils.get(key);

            log.debug("命中缓存{}", key);

            return JSON.parseArray(value, Chongwu.class);
        }

        List<Chongwu> list = chongwuMapper.selectList(null);

        putList(list);

        return list;
    }

    @Override
    public void deleteList() {
        String key = RedisKeyPrefixes.PREFIX_CHONGWUS;

        redisUtils.delete(key);
    }

    @Override
    public void deleteById(String primaryKey) {
        redisUtils.delete(RedisKeyPrefixes.PREFIX_CHONGWU + primaryKey);
    }

    @Override
    public void delayDoubleDelete(String primaryKey, Consumer<String> consumer) {
        deleteList();
        deleteById(primaryKey);

        consumer.accept(primaryKey);

        // 延时双删
        TimerUtils.schedule(new TimerTask() {
            @Override
            public void run() {
                deleteList();
                deleteById(primaryKey);
            }
        });
    }

}